/**
 * Created by lxl on 2017/2/15.
 */

function autoIFrameHeight() {
    var iFrame = $('.layui-show iframe',parent.document)[0] ;
    if(isNotNull(iFrame)){
        var sub = document.body ;
        if(isNotNull(sub) && iFrame.height < sub.scrollHeight ) {
            iFrame.height = sub.scrollHeight ;
        }else{
            iFrame.height = sub.offsetHeight ;
        }
    }else{
        return ;
    }
}

function autoInject(Object){
    if(isNotNull(Object)) {
        var key = "" ;
        $.each(Object,function(i,val){
            key = '#' + i ;
            $(key).val(val) ;
            if($(key).is('[type="checkbox"]')){
                $(key).attr("checked",val) ;
            }
        }) ;
    }
}

function autoFetch(Object){
    if(isNotNull(Object)){
        $('input,select').each(function(index,element){
            if( isNotNull(element.id) && isNotNull(element.value) )
            Object[element.id] = element.value ;
        }) ;
    }
    return Object ;
}

function createOptions(id,Object,value,text){
    if(isNotNull(Object)){
        var element = $('#'+id) ;
        if(isNotNull(element)){
            $.each(Object,function(i,val){
                element.append('<option value="' + val[value] + '">' + val[text] + '</option>') ;
            }) ;
        }
    }
}

function isNotNull(Object){
    return Object !== undefined && Object !== null && Object !== "" ;
}

function createTag(param){
    var content = "" ;
    content = '<form class="layui-form"><div class="layui-form-item">'
                +' <div class="layui-inline">'
                +'<label class="layui-form-label">' +param.description + '</label>'
                +'<div class="layui-input-inline">'
                +'<input class="layui-input" disabled/>'
                +'</div></div></div></form>';
    return content ;
}

function createCustomerTag(param){
    var content = "" ;
    var verify = 'lay-verify="'+param.kind ;
    if(param.required){
        verify+='|required"' ;
    }else{
        verify+='"' ;
    }
    content = '<div class="layui-inline">'
                +'<label class="layui-form-label">' +param.description + '</label>'
                +'<div class="layui-input-inline">'
                +'<input id="'+param.tagLabelId+'" class="layui-input layui-tag" '+verify+'/>'
                +'</div></div>'
    return content ;
}

function createTagList(param){
    var content = '<label class="layui-form-label">标签列表</label>'
                    +'<div class="layui-input-inline">'
                    +'<select class="tagList">' ;
    content += '<option value=""></option>' ;
    $.each(param.list,function(i,val){
        content += '<option value="'+ val.id + '">' + val.name + '</option>' ;
    }) ;
    content += '</select></div>' ;
    return content ;
}

function createFlowList(param){
    var content = '<label class="layui-form-label">步骤列表</label>'
        +'<div class="layui-input-inline">'
        +'<select class="flowList">' ;
    content += '<option value=""></option>' ;
    $.each(param.list,function(i,val){
        content += '<option value="'+ val.id + '">' + val.name + ':'+val.description + ' </option>' ;
    }) ;
    content += '</select></div>' ;
    return content ;
}

function createcollaItem(param){
    var content = '<div class="layui-colla-item"><h2 class="layui-colla-title">'+param.description+'</h2>'
                    +'<div class="layui-colla-content layui-show">'
                    +'<div class="layui-form-item">';
    $.each(param.tags,function(i,val){
        content+= createCustomerTag(val) ;
    }) ;
    content+= '</div></div></div></div>' ;
    return content ;
}

function createCollaFlowItem(param){
    var content = '' ;
    $.each(param.flows,function(i,val){
        createFlowItem(val) ;
    }) ;
    return content ;
}

function createFlowItem(val){
    if(!isNotNull(val)){
        return "" ;
    }
    var content = '<div class="layui-form-item" id="'+val.id+'"> <label class="layui-form-label">'+val.name+'</label> <div class="layui-input-block"> <input class="layui-input" disabled value="'+val.description+'"/> </div> </div>' ;
    return  content ;
}

function createCollaMarketInfoItem(param){
    var content = '' ;
    $.each(param.marketInfos,function(i,val){
        content+=createMarketInfoItem(val) ;
    }) ;
    return content ;
}

function createMarketInfoItem(val){
    if(!isNotNull(val)){
        return "" ;
    }
    var content = '' ;
    var checked = "" ;
    if(val.value){
        checked = 'value="true" checked' ;
    }else{
        checked='value="false"';
    }
    content += '<div class="layui-form-item"><div><div class="layui-input-inline layui-input-inline-checkbox">';
    content +='<input id="'+val.id+'" class="layui-input" type="checkbox" lay-skin="primary" '+checked+'/></div>';
    content +='<label class="layui-form-label layui-label-left">'+val.name+'</label>';
    content +='<label class="layui-form-label layui-label-right">'+val.description+'</label></div></div>';
    return content ;
}
